/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.testis.gui.mvc.view.labels;

import org.testis.gui.mvc.utilities.TristateButtonModel;
import org.testis.gui.mvc.utilities.TristateCheckBox;
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import javax.swing.JTree;
import javax.swing.UIManager;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeCellRenderer;

import org.testis.utils.tree.TristateLabelTreeNode;

/**
 *
 * @author selcuk
 */
public class TristateLabelTreeNodeRenderer implements TreeCellRenderer {

    private TristateLabelTreeNode m_labelTreeNode;
    private TristateCheckBox m_renderer = new TristateCheckBox("");
    Color m_selectionBorderColor, m_selectionForeground, m_selectionBackground, m_textForeground, m_textBackground;

    public TristateLabelTreeNodeRenderer() {
        Font fontValue;
        fontValue = UIManager.getFont("Tree.font");
        if (fontValue != null) {
            m_renderer.setFont(fontValue);
        }
        Boolean booleanValue = (Boolean) UIManager.get("Tree.drawsFocusBorderAroundIcon");
        m_renderer.setFocusPainted((booleanValue != null) && (booleanValue.booleanValue()));

        m_selectionBorderColor = UIManager.getColor("Tree.selectionBorderColor");
        m_selectionForeground = UIManager.getColor("Tree.selectionForeground");
        m_selectionBackground = UIManager.getColor("Tree.selectionBackground");
        m_textForeground = UIManager.getColor("Tree.textForeground");
        m_textBackground = UIManager.getColor("Tree.textBackground");
    }

    protected TristateCheckBox getRenderer() {
        return m_renderer;
    }

    protected TristateLabelTreeNode getLabelTreeNode() {
        return m_labelTreeNode;
    }

    @Override
    public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
        String stringValue = tree.convertValueToText(value, selected,
                expanded, leaf, row, false);
        m_renderer.setText(stringValue);
        m_renderer.setSelected(false);
        m_renderer.setEnabled(tree.isEnabled());

        if (selected) {
            m_renderer.setForeground(m_selectionForeground);
            m_renderer.setBackground(m_selectionBackground);
        } else {
            m_renderer.setForeground(m_textForeground);
            m_renderer.setBackground(m_textBackground);
        }

        if ((value != null) && (value instanceof DefaultMutableTreeNode)) {
            Object userObject = ((DefaultMutableTreeNode) value).getUserObject();
            if (userObject instanceof TristateLabelTreeNode) {
                m_labelTreeNode = (TristateLabelTreeNode) userObject;
                m_renderer.setText(m_labelTreeNode.getName());
                m_renderer.setModel(new TristateButtonModel(m_labelTreeNode.getState()));

                Color text_color = Color.BLACK;
                switch (m_renderer.getState()) {
                    case SELECTED:
                        text_color = Color.BLUE;
                        break;
                    case INDETERMINATE:
                        text_color = Color.RED;
                        break;
                    default:
                        break;
                }
                m_renderer.setForeground(text_color);
            }
        }
        return m_renderer;
    }
}
